<?php
namespace Admin\Model;
use Common\Model\CommonModel;
class WeixinMenuModel extends  CommonModel{
	
	//自动验证
	protected $_validate = array(
			//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
			array('name', 'require', '菜单名称不能为空！', 1, 'regex', 3),
			array('type', 'require', '菜单类型不能为空！', 1, 'regex', 1),



		
	);


	public function get_menus(){

		$top_menu=$this->where(array("parentid"=>0))->order("listorder desc")->select();


		foreach($top_menu as $k =>$v){



			$sub_menu=$this->where(array("parentid"=>$v["id"]))->order("listorder desc")->select();



			foreach($sub_menu as $key =>$s){



				$sub_button[$k][]=array(

					"name"=>$s["name"],
					"type"=>$s["type"],
					"key"=>$s["key"],
					"url"=>$s["url"],
					"media_id"=>$s["media_id"]
				);



			}

			if(count($sub_menu)==0){


				$button[]=array(
					"name"=>$v["name"],
					"type"=>$v["type"],
					"key"=>$v["key"],
					"url"=>$v["url"],
					"media_id"=>$v["media_id"]
				);



			}else{
				$button[]=array(
					"name"=>$v["name"],
					"sub_button"=>$sub_button[$k]
				);


			}






		}

		return $button;





	}



	
	
	/**
	 * 后台有更新/编辑则删除缓存
	 * @param type $data
	 */
	public function _before_write() {
		parent::_before_write();
		$this->data_cache();
	}

	public function _after_insert() {
		parent::_after_insert();
		$this->data_cache();
	}

	//删除操作时删除缓存
	public function _after_delete() {
		parent::_after_delete();
		$this->data_cache();
	}

	function data_cache(){}

	
}